﻿using BLL;
using Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using YunProject.Controllers.Base;

namespace YunProject.Controllers
{
    public class ProductSetController : BaseController
    {
        ProductSetBLL setBLL;
        public ProductSetController()
        {
            setBLL = new ProductSetBLL();
        }

        #region
        [Authorize]
        public IActionResult GongXuInfo()
        {
            return View();
        }
        [Authorize]
        public IActionResult GxFormSet()
        {
            return View();
        }
        /// <summary>
        /// 工艺标准
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public IActionResult GyStandard()
        {
            return View();
        }
        [Authorize]
        public IActionResult UpdateGyStandard()
        {
            return View();
        }
        /// <summary>
        /// 加载工序表单
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public IActionResult ProductProcessForm()
        {
            return View();
        }
        [Authorize]
        public IActionResult SelectGongYi()
        {
            return View();
        }

        [Authorize]
        public IActionResult UpdateGongXu()
        {
            return View();
        }
        [Authorize]
        public IActionResult GxGroup()
        {
            return View();
        }
        [Authorize]
        public IActionResult UpdateGxGroup()
        {
            return View();
        }
        [Authorize]
        public IActionResult GongXuSetUser()
        {
            return View();
        }
        
        #endregion


        [HttpPost]
        public JsonResult UpdateGxForm(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.UpdateGxForm(datas, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult SaveGyStandard(string mData, string fData)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.SaveGyStandard(mData, fData, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult DelGyStandard(string keyCode, string gxCode, string relaitionCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.DelGyStandard(keyCode, gxCode, relaitionCode, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult GetProcessProduct()
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.GetProcessProduct();
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult loadProcessGx(string reCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.loadProcessGx(reCode);
            }
            return Json(rsl);
        }

        /// <summary>
        /// 加载计划工序表单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult loadPlanProcessGx(string plCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.loadPlanProcessGx(plCode);
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult loadGxFormByCode(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.loadGxForm(datas);
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult UpdateGongXu(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.UpdateGongXu(datas, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult AddGongXuUser(string userCodes,string gxCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.AddGongXuUser(userCodes,gxCode, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult DelGongXuUser(string userCode, string gxCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = setBLL.DelGongXuUser(gxCode,userCode);
            }
            return Json(rsl);
        }
        
        [HttpPost]
        public JsonResult GetStandardByCode(string datas)
        {
            return Json(setBLL.GetStandardByCode(datas));
        }
        [HttpPost]
        public JsonResult UpdateStatus(string cCode, string status)
        {
            return Json(setBLL.UpdateStatus(cCode, status));
        }

        [HttpPost]
        public JsonResult SaveGyMainData(string datas)
        {
            return Json(setBLL.SaveGyMainData(datas));
        }

       

        [HttpGet]
        public JsonResult getDataList(string page, string limit, string condition, string dataType)
        {
            var rsl = new ReturnResult();
            string pageCount = "1";
            string msg = "";
            var datas = new object();
            var userObj = LoginInfo();
            if (dataType == "gxlist")
            {
                rsl = setBLL.GetGongXuList(condition);
            }
            else if (dataType == "gxf")
            {
                rsl = setBLL.GetGongFormByCode(page, limit, ref pageCount, condition);
            }
            else if (dataType == "gsp")
            {
                rsl = setBLL.ProductGyStandardList(page, limit, ref pageCount, condition);
            }
            else if (dataType == "proGyStandard")
            {
                rsl = setBLL.ProGyStandardList(page, limit, ref pageCount, condition); //存货/大类的工艺标准（选择工艺用）
            }
            else if (dataType == "group")
            {
                rsl = setBLL.GetGxGroupList(page, limit, ref pageCount, condition);
            }
            else if (dataType == "gxus")
            {
                rsl = setBLL.GongXuUserList(page, limit, condition, ref pageCount);
            }
            if (rsl.Result)
            {
                datas = rsl.Data;
            }
            else
            {
                msg = rsl.Message;
            }
            var dic = new Dictionary<string, object>();
            dic["count"] = pageCount;
            dic["msg"] = msg;
            dic["code"] = "0";
            dic["data"] = datas;
            return new JsonResult(dic);

        }
        [HttpPost]
        public JsonResult DelFGXData(string dataId)
        {
            return Json(setBLL.DelFGXData(dataId));
        }
    }
}
